import isPlainObject from 'lodash/isPlainObject';
import { mapNodes, getUniqueID } from '@/utils/utils';

// eslint-disable-next-line import/prefer-default-export
export function serialize(tree) {
  return mapNodes(tree, node => {
    if (typeof node.id !== 'string') {
      node.id = getUniqueID('_id');
    }

    if (!isPlainObject(node.state)) {
      node.state = {
        expanded: true,
      };
    }

    return node;
  });
}
